package org.github.manx98.jlibhac.tools.ncm;

import lombok.Data;
import org.github.manx98.jlibhac.fs.IStorage;

import java.io.IOException;

/**
 * @author Manx98
 * @version 1.0
 * @since 2022/10/6 18:40
 */
@Data
public class CnmtExtended {
    public int PrevMetaCount;
    public int PrevDeltaSetCount;
    public int DeltaSetCount;
    public int FragmentSetCount;
    public int PrevContentCount;
    public int DeltaContentCount;

    public CnmtPrevMetaEntry[] PrevMetas;
    public CnmtPrevDelta[] PrevDeltaSets;
    public CnmtDeltaSetInfo[] DeltaSets;
    public CnmtFragmentSetInfo[] FragmentSets;
    public CnmtPrevContent[] PrevContents;
    public CnmtContentEntry[] DeltaContents;
    public FragmentMapEntry[] FragmentMap;
    public CnmtExtended(IStorage reader) throws IOException {
        PrevMetaCount = reader.readInt(); // Lists all previous content meta files
        PrevDeltaSetCount = reader.readInt(); // Lists all previous delta sets
        DeltaSetCount = reader.readInt(); // Lists the delta set for the current title version
        FragmentSetCount = reader.readInt(); // Groups fragments into full deltas
        PrevContentCount = reader.readInt(); // Lists all previous NCAs for the title
        DeltaContentCount = reader.readInt(); // Lists all NCAs containing delta fragments
        reader.setPosition(reader.getPosition() + 4);

        PrevMetas = new CnmtPrevMetaEntry[PrevMetaCount];
        PrevDeltaSets = new CnmtPrevDelta[PrevDeltaSetCount];
        DeltaSets = new CnmtDeltaSetInfo[DeltaSetCount];
        FragmentSets = new CnmtFragmentSetInfo[FragmentSetCount];
        PrevContents = new CnmtPrevContent[PrevContentCount];
        DeltaContents = new CnmtContentEntry[DeltaContentCount];

        for (int i = 0; i < PrevMetaCount; i++)
        {
            PrevMetas[i] = new CnmtPrevMetaEntry(reader);
        }

        for (int i = 0; i < PrevDeltaSetCount; i++)
        {
            PrevDeltaSets[i] = new CnmtPrevDelta(reader);
        }

        for (int i = 0; i < DeltaSetCount; i++)
        {
            DeltaSets[i] = new CnmtDeltaSetInfo(reader);
        }
        int fragmentCount = 0;
        for (int i = 0; i < FragmentSetCount; i++)
        {
            FragmentSets[i] = new CnmtFragmentSetInfo(reader);
            fragmentCount += FragmentSets[i].FragmentCount;
        }

        for (int i = 0; i < PrevContentCount; i++)
        {
            PrevContents[i] = new CnmtPrevContent(reader);
        }

        for (int i = 0; i < DeltaContentCount; i++)
        {
            DeltaContents[i] = new CnmtContentEntry(reader);
        }
        FragmentMap = new FragmentMapEntry[fragmentCount];

        for (int i = 0; i < fragmentCount; i++)
        {
            FragmentMap[i] = new FragmentMapEntry(reader);
        }
    }
}
